<?php
$mode = $_GET["mode"];
//mysql start
$servername = "localhost";
$username = "jdt";
$password = "tJ732412jdt";
$database = "jdt";
 
// 创建连接
$conn = mysqli_connect($servername, $username, $password,$database);
 $a=array();
// 检测连接
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
 // echo "<script>console.log('连接成功')</script>";


//mysql
switch($mode){
	case "chaxun":
		chaxun();
		echo json_encode($a,true);
		exit ("");
	break;
	case "creat":
		creat();
		exit ("");
	break;
	case "modify":
		modify();
		exit ("");
	break;
	case "modify2":
		modify2();
		exit ("");
	break;
}
function chaxun(){
	global $a;
	$sql = "SELECT * FROM elist";
	$result = ssql($sql);
	while($row = $result->fetch_assoc()) {
	        // echo "id: " . $row["id"]. " - Name: " . $row["name"]. "-percentage: " . $row["percentage"]. "<br>";
	        $tempArray = array($row["id"],$row["name"],$row["percentage"],$row["color"]);
	        array_push($a,$tempArray);
	    }
	
}
function ssql(&$sql){
	global $conn;
	$result = $conn->query($sql);
	 
	if ($result->num_rows > 0) {
	    // 输出数据
	    return $result;
	} else {
	    // echo "0 结果";
	 echo "<script>alert('0 结果')</script>";

	}
}
function ssql2(&$sql){
	global $conn;
	if ($conn->query($sql) === TRUE) {
	     // echo "新记录插入成功";
	} else {
	     // echo "Error: " . $sql . "<br>" . $conn->error;
	}
}

function modify(){
	$pid = $_POST["pid"];
	$name = $_POST["name"];
	$percentage = $_POST["percentage"];
	$color = $_POST["color"];
	$sql="UPDATE elist SET name='$name',percentage='$percentage',color='$color'
WHERE id='$pid'";
	ssql2($sql);
// http://101.201.71.112:23333/jdt.php?mode=modify&pid=1&name=高数修改&percentage=21&color=2
}
function modify2(){
	$pid = $_POST["pid"];
	$percentage = $_POST["percentage"];
	$sql="UPDATE elist SET percentage='$percentage'
WHERE id='$pid'";
	ssql2($sql);
}
function creat(){
	$name = $_POST["name"];
	$percentage = $_POST["percentage"];
	$color = $_POST["color"];
	$sql="INSERT INTO elist (name, percentage, color)
VALUES ('$name', '$percentage', '$color')";
	ssql2($sql);
// http://101.201.71.112:23333/jdt.php?mode=creat&name=创建测试&percentage=100&color=2
}
$conn->close();
?>
